/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package famtime.tm.kalender.business;

import famtime.tm.kalender.model.Event;
import famtime.tm.kalender.model.FamUser;
import famtime.tm.kalender.model.Kalender;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Marius
 */
@Stateless
public class KalenderService {

    @PersistenceContext
    private EntityManager em;

    public void create(Kalender k) {
        em.persist(k);
    }

    public void update(Kalender k) {
        em.merge(k);
    }

    public void remove(Kalender k) {
        em.remove(k);
    }

    public Kalender findByName(String name) throws NoSuchKalenderException {
        List<Kalender> resultList = em.createNamedQuery(Kalender.FIND_BY_NAME, Kalender.class)
                .setParameter("name", name)
                .getResultList();
        if (resultList != null && resultList.size() > 1) {
            throw new IllegalStateException("kalender named " + name + " was found more than once.");
        }
        if (resultList != null && resultList.size() == 1) {
            return resultList.get(0);
        }
        throw new NoSuchKalenderException("kalender " + name + " doesn't exist.");
    }

    public Kalender addEvent(Kalender k, String titel) {
        k = em.merge(k);
        Event e = new Event(titel);
        k.addEvent(e);
        return k;
    }

    public Kalender removeEvent(Kalender k, Event e) {
        k = em.merge(k);
        k.remove(e);
        return k;
    }

    
    
    public List<Kalender> getAll() {
        TypedQuery<Kalender> query = em.createQuery("select c from Kalender c", Kalender.class);

        return query.getResultList();
    }

    public List<Kalender> findAll() {
        return em.createNamedQuery(Kalender.FIND_ALL, Kalender.class).getResultList();
    }

    public FamUser login(String username, String password) {
        List<FamUser> resultList;
        resultList = em.createQuery("select k from FamUser k where k.username=:username and k.password=:password")
                .setParameter("username", username)
                .setParameter("password", password)
                .getResultList();

        if(resultList.isEmpty()) {
            return null;
        }
        else {
            return resultList.get(0);
        }
    }
    
    public FamUser register(String username, String password) {
        List<FamUser> resultList;
        resultList = em.createQuery("select k from FamUser k where k.username=:username and k.password=:password")
                .setParameter("username", username)
                .setParameter("password", password)
                .getResultList();

        if(resultList.isEmpty()) {
            return addUser(username, password);
        }
        else {
            return null;
        }
    }
    
    public FamUser addUser(String username, String password){
        FamUser f = new FamUser(username, password);
        return f;
    }
}
